package lac.serviceuint.cfg;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
/**
 * 服务单元
 * @author zhiyi.zhao
 *
 */
@XStreamAlias("serviceunit")
public class ServiceUnit implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6562849355517113260L;
	
	@XStreamAsAttribute
	private String id;
	@XStreamAsAttribute
	private String name;
	@XStreamAsAttribute
	private String describe;
	@XStreamAsAttribute
	private Integer sort;
	@XStreamImplicit(itemFieldName="method")
	private List<Method> methods = new ArrayList<Method>();
	@XStreamOmitField
	private Map<String, Method> methodMap ;
	
	public ServiceUnit() {
		// TODO Auto-generated constructor stub
	}
	
	public ServiceUnit(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescribe() {
		return describe;
	}

	public void setDescribe(String describe) {
		this.describe = describe;
	}

	public List<Method> getMethods() {
		return methods;
	}

	public void setMethods(List<Method> methods) {
		this.methods = methods;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}
	
	public Method getMethod(String name){
		if(methodMap==null){
			methodMap = new LinkedHashMap<String, Method>();
			for(Method method : methods){
				methodMap.put(method.getName(), method);
			}
		}
		return methodMap.get(name);
	}
	
	public Method getMethod(Method method){
		return getMethod(method.getName());
	}
	
	public ServiceUnit original(){
		ServiceUnit serviceUnit = new ServiceUnit();
		serviceUnit.setId(id);
		serviceUnit.setName(name);
		serviceUnit.setDescribe(describe);
		serviceUnit.setSort(sort);
		serviceUnit.setMethods(methods);
		return serviceUnit;
	}
}
